<?php
$mod = 'blank';
include("../api.inc.php");
$title = '卡密生成 - 年卡';
include './head.php';
if ($islogin == 1) {
} else exit("<script language='javascript'>window.location.href='./login.php';</script>");
$num = $_POST['num'];
function getkmkey($len = 16)
{
    $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    $strlen = strlen($str);
    $randstr = '';
    for ($i = 0; $i < $len; $i++) {
        $randstr .= $str[mt_rand(0, $strlen - 1)];
    }
    return $randstr;
}

?>
    <div class="col-lg-8 col-md-12 col-lg-offset-2 text-center">
    <div class="panel panel-info" draggable="true">
        <div class="panel-heading font-bold">卡密查询</div>
        <div class="panel-body">
            <form method="GET" action="">
                <input type="hidden" name="page" value="1">
                <div class="input-group">
                    <span class="input-group-addon">卡密内容</span>
                    <input type="text" name="code" class="form-control" placeholder="输入需要查询的卡密">
                </div>
                <br/>
                <div class="col-sm-12"><input type="submit" value="查询" class="btn btn-primary form-control"/>
                </div>
            </form>
        </div>
        <br/>
            <div class="panel-heading font-bold">卡密生成</div>
            <div class="panel-body">
                <form method="post" action="">
                    <input type="hidden" name="do" value="do">
                    <div class="input-group">
                        <span class="input-group-addon">卡密个数</span>
                        <input type="text" name="num" class="form-control" placeholder="输入需要生成的个数">
                    </div>
                    <br/>
                    <div class="col-sm-12"><input type="submit" value="确认生成" class="btn btn-primary form-control"/>
                    </div>
                </form>
            </div>
            <div class="panel-footer text-center">请填写生成卡密数量</div>
        </div>
    </div>
    <div class="col-sm-2"></div>
    <div class="col-lg-8 col-md-12 col-lg-offset-2 text-center">
    <div class="panel panel-primary">
        <div class="panel-heading">生成结果</div>
        <div class="panel-body">
            <?php
            if ($_POST['do'] == 'do') {
                if ($num != '') {
                    $generatedKeys = array(); // 保存生成的卡密
                    for ($i = 1; $i <= $num; $i++) {
                        $key = getkmkey();
                        $DB->query("INSERT INTO `auth_kms` (`km`, `zt`, `type`) VALUES ('$key', '待激活','ClassB_365')");
                        $generatedKeys[] = $key; // 将生成的卡密添加到数组中
                    }

                    // 显示生成的卡密
                    echo "<textarea id='generatedKeys' rows='10' cols='50'>";
                    echo implode("\n", $generatedKeys);
                    echo "</textarea>";
                    echo "<button onclick='copyKeys()'>复制</button>";

                    // 点击确定按钮后复制卡密的JavaScript函数
                    echo "<script>
                function copyKeys() {
                    var copyText = document.getElementById('generatedKeys');
                    copyText.select();
                    document.execCommand('copy');
                    alert('卡密已复制到剪贴板');
                }
              </script>";
                }
            }
            ?>
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>卡密</th>
                        <th>状态</th>
                        <th>激活时间</th>
                        <th>到期时间</th>
                        <th>激活IP</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    $pagesize = 20;

                    if (!isset($_GET['page'])) {
                        $page = 1;
                    } else {
                        $page = $_GET['page'];
                    }
                    $pageu = ($page - 1) * $pagesize;
                    if ($_GET['code']) {
                        $code = $_GET['code'];
                        $rs = $DB->query("SELECT * FROM auth_kms WHERE `type` = 'ClassB_365' and `km` = '" . $code . "'");
                    } else {
                        $rs = $DB->query("SELECT * FROM auth_kms WHERE `type` = 'ClassB_365' ORDER BY id DESC LIMIT $pageu, $pagesize");
                    }
                    //$rs = $DB->query("SELECT * FROM auth_kms WHERE `type` = 'ClassB_365' order by id desc limit $pageu,$pagesize");
                    while ($res = $DB->fetch($rs)) {
                        $daili_id = $res['daili_id'];
                        $user_rs = $DB->query("SELECT dlqq FROM auth_user WHERE uid = $daili_id");
                        $user_res = $DB->fetch($user_rs);
                        $dlqq = $user_res['dlqq'];
                        echo '<tr>
            <td>' . $res['id'] . '</td>
            <td>' . $res['km'] . '</td>
            <td>' . $res['zt'] . '</td>
            <td>' . date('Y-m-d', $res['activation']) . '</td>
            <td>' . date('Y-m-d', $res['Expiration']) . '</td>
            <td>' . $dlqq . '</td>
            <td>' . $res['VisitorIp'] . '</td>
          </tr>';
                    }
                    ?>
                    </tbody>
                </table>
            </div>
            <?php
            echo '<ul class="pagination">';
            $s = ceil($gls / $pagesize);
            $first = 1;
            $prev = $page - 1;
            $next = $page + 1;
            $last = $s;
            if ($page > 1) {
                echo '<li><a href="ClassB_365.php?page=' . $first . $link . '">首页</a></li>';
                echo '<li><a href="ClassB_365.php?page=' . $prev . $link . '">«</a></li>';
            } else {
                echo '<li class="disabled"><a>首页</a></li>';
                echo '<li class="disabled"><a>«</a></li>';
            }
            for ($i = 1; $i < $page; $i++)
                echo '<li><a href="ClassB_365.php?page=' . $i . $link . '">' . $i . '</a></li>';
            echo '<li class="disabled"><a>' . $page . '</a></li>';
            for ($i = $page + 1; $i <= $s; $i++)
                echo '<li><a href="ClassB_365.php?page=' . $i . $link . '">' . $i . '</a></li>';
            echo '';
            if ($page > $s) {
                echo '<li><a href="ClassB_365.php?page=' . $next . $link . '">»</a></li>';
                echo '<li><a href="ClassB_365.php?page=' . $last . $link . '">尾页</a></li>';
            } else {
                echo '<li class="disabled"><a>»</a></li>';
                echo '<li class="disabled"><a>尾页</a></li>';
            }
            echo '</ul>';
            #分页
            ?>
        </div>
    </div>
<?php include './foot.php'; ?>